package com.team2.outpatient_sys.web.pharmacyControllers;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team2.outpatient_sys.pojo.*;
import com.team2.outpatient_sys.service.PharmacyInService;
import com.team2.outpatient_sys.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 实现库房配送药品，根据配送表来接收药品，
 * 没有问题则按照数量放入药房上架，需要点击确认按钮
 * 有问题则点击修改把没有问题的药品和数量放到药房
 * 点击问题按钮把有问题的药品和数量放入药品问题表中
 * 点击上架，把信息存入库存表中
 * */

@RequestMapping("/medicines")
@RestController
public class PharmacyInController {
    @Autowired
    private PharmacyInService pharmacyInService;
    //展示所有未接收的药品
    @GetMapping("/showAllDistribution/{currentPage}/{page}")
    public Result showAllDistribution(@PathVariable Integer currentPage,@PathVariable Integer page){
        PageHelper.startPage(currentPage, page);

        Result result = new Result();
        List<ShowDistribution> all = pharmacyInService.findAllDistribution();

        PageInfo<ShowDistribution> allDistribution = new PageInfo<>(all);

        if (allDistribution == null){
            result.setCode(500);
            result.setMsg("查询失败");
        }else{
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(allDistribution);
        }
        return result;
    }

    //展示所有已经接收的药品
    @GetMapping("/showAllDistributionYes/{currentPage}/{page}")
    public Result showAllDistributionYes(@PathVariable Integer currentPage,@PathVariable Integer page){
        PageHelper.startPage(currentPage, page);

        Result result = new Result();
        List<ShowDistribution> all = pharmacyInService.findAllDistributionYes();

        PageInfo<ShowDistribution> allDistribution = new PageInfo<>(all);

        if (allDistribution == null){
            result.setCode(500);
            result.setMsg("查询失败");
        }else{
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(allDistribution);
        }
        return result;
    }

    //药品没问题接收该药品,放入入库表中
    @PostMapping("/acceptance/{name}")
    public Result Acceptance(@RequestBody StorageIn storageIn ,@PathVariable String name){
        Result result = new Result();
        //调用日期工具类获取一个时间
        String dateNowStr = DateUtils.getDate();
        storageIn.setInTime(dateNowStr);

        storageIn.setShelves("否");
        Integer integer = pharmacyInService.storageIn(storageIn,name);
        if (integer < 1 ){
            result.setCode(500);
            result.setMsg("入库失败");
        }else {
            result.setCode(200);
            result.setMsg("入库成功");
            result.setData(integer);
        }
        return result;
    }

    //药品出现问题接收该药品,说明问题放入问题表中
    @PostMapping("/issue")
    public Result addIssue(@RequestBody UnqualifiedMedicine unqualifiedMedicine){
        Result result = new Result();
        //获取一个执行的时间
        unqualifiedMedicine.setRecordTime(DateUtils.getDate());

        Integer add = pharmacyInService.add(unqualifiedMedicine);

        if (add < 1 ){
            result.setCode(500);
            result.setMsg("添加失败");
        }else {
            result.setCode(200);
            result.setMsg("添加成功");
        }
        return result;
    }

    //展示所有需要上架的药品信息
    @GetMapping("/showAllStorageIn/{currentPage}/{page}")
    public Result showAll(@PathVariable Integer currentPage,@PathVariable Integer page){
        PageHelper.startPage(currentPage,page);
        Result result = new Result();
        List<StorageInMedicine> all = pharmacyInService.findAllStorageIn();

        PageInfo<StorageInMedicine> pi = new PageInfo<>(all);

        if (pi == null){
            result.setCode(500);
            result.setMsg("查询失败");
        }else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pi);
        }
        return result;
    }

    //展示所有已经上架的药品信息
    @GetMapping("/showAllStorageInYes/{currentPage}/{page}")
    public Result showAllYes(@PathVariable Integer currentPage,@PathVariable Integer page){
        PageHelper.startPage(currentPage,page);
        Result result = new Result();
        List<StorageInMedicine> all = pharmacyInService.findAllStorageInYes();

        PageInfo<StorageInMedicine> pi = new PageInfo<>(all);

        if (pi == null){
            result.setCode(500);
            result.setMsg("查询失败");
        }else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pi);
        }
        return result;
    }

    //药品上架
    @PostMapping("/addPharmacyStorage")
    public Result addPharmacyStorage(@RequestBody PharmacyStorage pharmacyStorage){
        Result result = new Result();
        Integer add = pharmacyInService.addPharmacyStorage(pharmacyStorage);
        if (add < 1 ){
            result.setCode(500);
            result.setMsg("添加失败");
        }else {
            result.setCode(200);
            result.setMsg("添加成功");
        }
        return result;
    }
}
